package com.cavy.platform.server.service;

import com.cavy.platform.server.domain.Agent;
import com.cavy.platform.server.dto.AgentDeleteDto;
import com.cavy.platform.server.dto.AgentRegisterDto;
import com.cavy.platform.server.dto.AgentStatusDto;
import com.cavy.platform.server.mapper.AgentMapper;
import com.cavy.platform.server.repository.AgentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * agent管理器
 */
@Service
public class AgentService {
    @Autowired
    private AgentRepository repository;
    @Autowired
    private AgentSessionService sessionService;

    public void register(AgentRegisterDto dto) {
        repository.add(dto.getHostname(), AgentMapper.INSTANCE.convertToAgent(dto));
    }

    public void status(AgentStatusDto dto) {
        Agent agent = repository.findByHostname(dto.getHostName());
        agent.statusChange(dto);
        repository.save(agent);
        sessionService.changeStatus(dto.getHostName(), dto.getStatus());
    }

    public void delete(AgentDeleteDto dto) {
        repository.delete(dto.getHostname());
    }
}
